import { getSystemThemeList, getProductList, getFieldsList, getOrganList } from '@/api/system'
import { getSystemRoleList } from '@/api/system/roleConfig.js'

import { getLocalUserAuthority, setLocalUserAuthority } from '@/utils/service'
export default {
  namespaced: true,

  state: {
    userAuthority: getLocalUserAuthority(),

    themeList: [],
    productList: [],
    fieldList: [],
    organList: [],
    roleList: []
  },

  actions: {
    /* 查询主题分类列表 */
    async getSystemThemeList({ commit }) {
      const res = await getSystemThemeList()
      const data = res.data.data || []
      commit('SET_LIST', { field: 'themeList', data })
    },

    /* 查询产品分类列表 */
    async getProductList({ commit }) {
      const res = await getProductList()
      const data = res.data.data || []
      commit('SET_LIST', { field: 'productList', data })
    },

    /* 查询领域分类列表 */
    async getFieldsList({ commit }) {
      const res = await getFieldsList()
      const data = res.data.data || []
      commit('SET_LIST', { field: 'fieldList', data })
    },

    /* 机构选择列表 */
    async getOrganList({ commit }) {
      const res = await getOrganList()
      const data = res.data.data || []
      commit('SET_LIST', { field: 'organList', data })
    },

    /* 角色列表 */
    async getSystemRoleList({ commit }) {
      const res = await getSystemRoleList()
      const data = res.data.data || []
      commit('SET_LIST', { field: 'roleList', data })
    },
  },

  mutations: {
    SET_LIST(state, params) {
      state[params.field] = Object.freeze(params.data)
    },

    set_userAuthority(state, value) {
      state.userAuthority = value
      setLocalUserAuthority(value)
    },
  },
}
